package com.hightrast.service.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
//@Order(Ordered.HIGHEST_PRECEDENCE)
public class DruidDataSourceConfig {

    @Resource
//    private MyBatisConfig config;

    @Value("${mybatis-plus.mapper-locations}")
    private String mapperLocations;

    @Value("${mybatis-plus.typeAliasesPackage}")
    private String typeAliasesPackage;

//    @Resource
//    CustomIdGenerator customIdGenerator;


//    @Bean()
//    @ConfigurationProperties(prefix = "spring.datasource")
//    public DataSource masterDataSource() {
//        return DataSourceBuilder.create().build();
//    }

    @Bean
    @ConfigurationProperties(value = "spring.datasource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();

        return druidDataSource;

    }

    @Bean
    public SqlSessionFactory mainSqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
        sessionFactory.setPlugins(mybatisPlusInterceptor());
        //添加id生成器的配置
        sessionFactory.setGlobalConfig(globalConfig());
        return sessionFactory.getObject();
    }
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig conf = new GlobalConfig();
        conf.setIdentifierGenerator(new CustomIdGenerator());
        return conf;
    }
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
//        paginationInnerInterceptor.setOverflow(true);
//        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
//        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }



    @Bean
    public DataSourceTransactionManager mainTransactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SqlSessionTemplate mainSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

//    @Bean
//    public MybatisPlusPropertiesCustomizer mybatisPlusPropertiesCustomizer() {
//        return properties -> {
//            // 设置数据库类型
//            properties.getGlobalConfig().getDbConfig().setIdType(IdType.ASSIGN_ID);
//            properties.getGlobalConfig().setIdentifierGenerator(customIdGenerator);
//            // 设置数据库类型
//            properties.getConfiguration().setDatabaseId(DbType.MYSQL.name());
//        };
//    }
}

